package top.fengyulou.xiaofeng.controller.admin;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import top.fengyulou.xiaofeng.dto.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.fengyulou.xiaofeng.common.Jr;
import top.fengyulou.xiaofeng.common.UserUtils;
import top.fengyulou.xiaofeng.enums.PaperTypeEnum;
import top.fengyulou.xiaofeng.enums.QuestionTypeEnum;
import top.fengyulou.xiaofeng.service.PaperQuestionRuleService;
import top.fengyulou.xiaofeng.service.PaperQuestionService;
import top.fengyulou.xiaofeng.service.PaperService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台用户管理
 */
@Controller
public class AdminPaperController {

    @Resource
    private PaperService paperService;
    @Resource
    private PaperQuestionRuleService paperQuestionRuleService;
    @Resource
    private PaperQuestionService paperQuestionService;

    /**
     * 列表页面
     */
    @RequestMapping("/admin/paper/list")
    public String list() {
        return "admin/paper/admin-paper-list";
    }

    //编辑页面
    @RequestMapping("/admin/paper/save")
    public String savePage(String id, Model model) {
        if (StringUtils.isNotBlank(id)) {
            PaperDto paper = paperService.getById(id);
            model.addAttribute("data", paper);
        }
        return "admin/paper/admin-paper-save";
    }

    /**
     * 设置试题
     *
     * @return
     */
    @RequestMapping("/admin/paper/question/{paperId}")
    public String question(@PathVariable String paperId, Model model) {
        model.addAttribute("paperId", paperId);
        PaperDto byId = paperService.getById(paperId);
        model.addAttribute("paper", byId);
        if (byId.getType().equals(PaperTypeEnum.SUIJICHUTI.getCode())) {
            Map<String, Object> typeCountByPaperId = paperQuestionRuleService.getTypeCountByPaperId(paperId);
            model.addAttribute("typeCount", typeCountByPaperId);
            return "admin/paper/admin-paper-question-rule";
        }
        return "admin/paperQuestion/admin-paperQuestion-list";
    }

    /**
     * 设置试题
     *
     * @return
     */
    @RequestMapping("/admin/paper/child/ruleList/{paperId}")
    public String ruleList(@PathVariable String paperId, Model model) {
        PaperQuestionRuleDto query = new PaperQuestionRuleDto();
        query.setPaperId(paperId);
        List<PaperQuestionRuleDto> list = paperQuestionRuleService.list(query);
        model.addAttribute("list", list);
        return "admin/paper/admin-paper-rule-list-child";
    }

    /**
     * 设置用户
     *
     * @return
     */
    @RequestMapping("/admin/paper/user/{paperId}")
    public String user(@PathVariable String paperId, Model model) {
        model.addAttribute("paperId", paperId);
        return "admin/admin-paper-user";
    }

    /**
     * 修改页面
     */
    @RequestMapping("/admin/paper/update/{id}")
    public String update(@PathVariable String id, Model model) {
        model.addAttribute("id", id);
        return "admin/admin-paper-save";
    }

    /**
     * 选择用户页面
     */
    @RequestMapping("/admin/paper/select/user")
    public String selectUser() {
        return "admin/admin-paper-select-user";
    }

    /**
     * 试卷列表
     */
    @RequestMapping("/admin/paper/ajax/list")
    @ResponseBody
    public Jr list(@RequestBody PaperDto paperDto) {
        paperService.list(paperDto);
        return Jr.yesd(paperDto);
    }

    /**
     * 添加规则
     */
    @RequestMapping("/admin/paper/rule/ajax/add")
    @ResponseBody
    public Jr ajaxAddRule(PaperQuestionRuleDto paperQuestionRuleDto, HttpServletRequest request) {
        paperQuestionRuleDto.setCurrentUserId(UserUtils.getUserId(request));
        paperQuestionRuleService.insertBatch(paperQuestionRuleDto);
        //已选择的分类
        List<String> classifyIdListByPaperId = paperQuestionRuleService.getClassifyIdListByPaperId(paperQuestionRuleDto);
        String classifyIds = String.join(",", classifyIdListByPaperId);
        //分类数量
        Map<String, Object> typeCountByPaperId = paperQuestionRuleService.getTypeCountByPaperId(paperQuestionRuleDto.getPaperId());
        typeCountByPaperId.put("classifyIds",classifyIds);
        return Jr.yesd(typeCountByPaperId);
    }

    /**
     * 修改试卷规则分数
     */
    @RequestMapping("/admin/paper/rule/ajax/updateQstNum")
    @ResponseBody
    public Jr ajaxRuleUpdateQstNum(PaperQuestionRuleDto paperQuestionRuleDto, HttpServletRequest request) {
        paperQuestionRuleDto.setCurrentUserId(UserUtils.getUserId(request));
        paperQuestionRuleService.updateRuleQstNum(paperQuestionRuleDto);
        return Jr.yes();
    }

    /**
     * 修改试卷规则分数
     */
    @RequestMapping("/admin/paper/rule/ajax/updateQstScore")
    @ResponseBody
    public Jr ajaxRuleUpdateQstScore(PaperQuestionRuleDto paperQuestionRuleDto, HttpServletRequest request) {
        paperQuestionRuleDto.setCurrentUserId(UserUtils.getUserId(request));
        paperQuestionRuleService.updateRuleQstScore(paperQuestionRuleDto);
        return Jr.yes();
    }

    //查询试卷规则
    @RequestMapping("/admin/paper/rule/ajax/list")
    @ResponseBody
    public Jr ajaxRuleList(@RequestBody PaperQuestionRuleDto paperQuestionRuleDto) {
        paperQuestionRuleService.listPage(paperQuestionRuleDto);
        return Jr.yesd(paperQuestionRuleDto);
    }

    //查询试卷规则
    @RequestMapping("/admin/paper/rule/classifyIds")
    @ResponseBody
    public Jr ajaxRuleClassifyIds(@RequestParam String paperId, @RequestParam String qstType) {
        PaperQuestionRuleDto paperQuestionRuleDto = new PaperQuestionRuleDto();
        paperQuestionRuleDto.setPaperId(paperId);
        paperQuestionRuleDto.setQstType(qstType);
        List<String> classifyIdListByPaperId = paperQuestionRuleService.getClassifyIdListByPaperId(paperQuestionRuleDto);
        String classifyIds = "";
        if (ObjectUtils.isNotEmpty(classifyIdListByPaperId)) {
            classifyIds = String.join(",", classifyIdListByPaperId);
        }
        return Jr.yesd(classifyIds);
    }

    /**
     * 添加规则
     */
    @RequestMapping("/admin/paper/rule/ajax/delete")
    @ResponseBody
    public Jr ajaxRuleDelete(@RequestParam String[] ids,@RequestParam String paperId,HttpServletRequest request) {
        PaperQuestionRuleDto paperQuestionRuleDto = new PaperQuestionRuleDto();
        paperQuestionRuleDto.setCurrentUserId(UserUtils.getUserId(request));
        paperQuestionRuleDto.setIds(ids);
        paperQuestionRuleDto.setPaperId(paperId);
        paperQuestionRuleService.deleteExtend(paperQuestionRuleDto);
        Map<String, Object> typeCountByPaperId = paperQuestionRuleService.getTypeCountByPaperId(paperId);
        return Jr.yesd(typeCountByPaperId);
    }

    /**
     * 试题列表查询
     */
    @RequestMapping("/admin/paper/ajax/question/list/{paperId}")
    @ResponseBody
    public Jr ajaxQuestionList(@PathVariable String paperId) {
        QuestionDto questionDto = new QuestionDto();
        questionDto.setPaperId(paperId);
//        List<QuestionDto> questionDtoList = paperService.findQuestionList(questionDto);
        return Jr.yes();
    }

    /**
     * id查询
     */
    @RequestMapping("/admin/paper/ajax/info/{id}")
    @ResponseBody
    public Jr ajaxInfo(@PathVariable String id) {
        PaperDto paperDto = paperService.getById(id);
        return Jr.yesd(paperDto);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @RequestMapping("/admin/paper/ajax/save")
    @ResponseBody
    public Jr ajaxSave(HttpServletRequest request, PaperDto paperDto) {
        paperDto.setCurrentUserId(UserUtils.getUserId(request));
        paperService.save(paperDto);
        return Jr.yesd(paperDto);
    }

    /**
     * 删除
     */
    @RequestMapping("/admin/paper/ajax/delete")
    @ResponseBody
    public Jr delete(@RequestParam String[] ids) {
        paperService.delete(ids);
        return Jr.yes();
    }
}
